<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root">
    </div>
    <script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
    <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>


    <script type="text/babel">
       

        class InputElement extends React.Component{
            // state = {}
            input = React.createRef();
            constructor(props){
                super(props);
            }
           
            render(){
                return <div>
                    <label>{this.props.children}</label>
                    <input ref={this.input} type={this.props.type} value={this.props.value} onChange={this.props.onChange} onBlur={this.props.onBlur} />
                    <span>{this.props.info}</span>
                </div>
            }
        }
        class Reg extends React.Component{
            state = {
                username:{value:""},
                // pwd:{value:""},
                // confirm:{value:""}
            }
            pwd = React.createRef();
            validateUsername = (e) => {
                console.log(this.state.username);
                console.log(this.pwd.current.input.current.value);
                if(/^\w{6,20}$/.test(e.target.value)){
                    this.setState({                      
                        username:{
                            value:e.target.value,
                            info:"√"
                        }
                    });
                }else{
                    this.setState({
                        username:{
                            value:e.target.value,
                            info:"X"
                        }
                    });
                }
            }
            constructor(props){
                super(props);
            }
            render(){
                return <form action="#">
                        <h1>注册</h1>
                        <InputElement type="text"
                         value={this.state.username.value}
                         onChange={(e) => {console.log(e.target.value);this.setState({username:{value:e.target.value}})}}
                         onBlur={this.validateUsername}
                         >用户名：</InputElement>
                        <InputElement ref={this.pwd} type="password">密码：</InputElement>
                        <InputElement type="password">确认密码：</InputElement>
                        <button type="button">提交</button>
                    </form>
            }
        }
        
        const root = ReactDOM.createRoot(document.getElementById("root"));
        root.render(<Reg></Reg>);
    </script>
</body>
</html>